package com.example.administrator.appli;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.TextView;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;


public class Tab123Activity extends Activity {
    String TAG = "Tab123Activity";
    TextView tv_assets;
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

         tv_assets = (TextView) findViewById(R.id.tv_assets);
//        filedemo();
//        assetRaw(tv_assets);

        Log.e(TAG, "onCreate: " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "该应用没有设置相关权限", Toast.LENGTH_LONG).show();
                Log.e(TAG, "onCreate: 该应用没有设置相关权限");
                AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("没有读取联系人权限");
                dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Tab123Activity.this.finish();

                    }
                });
                dialog.show();
            }else {
                readContacts();
            }


        } else {
            readContacts();

        }
        final int[] i = {0};
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
//                readContacts();
                Log.e(TAG, "run: "+ i[0]++);
            }
        },10,10);


    }

    /**
     * 读取手机联系人
     */
    private void readContacts() {
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        String content="";
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.e(TAG, "onCreate: " + name + "," + number);
            content+=name + "," + number+"\n";
        }
        tv_assets.setText(content+ "");
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {


        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void assetRaw(TextView tv_assets) {
        AssetManager manager = getResources().getAssets();
        byte b[] = new byte[1000];
        try {
            //
            InputStream in = manager.open("note.txt");
            in.read(b);
            Log.e(TAG, "onCreate:asset " + new String(b));
            tv_assets.setText(new String(b));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取raw下文件 不包含其他目录，直接使用
        InputStream in = getResources().openRawResource(R.raw.note);
        try {
            in.read(b);
            Log.e(TAG, "onCreate:raw " + new String(b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void filedemo() {
        String path = Environment.getExternalStorageDirectory() + File.separator + this.getPackageName() + File.separator;
        String fileName = "123.txt";
        File f = new File(path);
        if (!f.exists()) {
            f.mkdir();
        }
        File file = new File(path + fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file, true);
            out.write("123".getBytes());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
